package org.fas.net
{
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	

	public class FLoader extends Loader
	{
		public var lastUrl:String = "";
		private var result:Function;
//		public var _loader:Loader = new Loader();
		public function FLoader()
		{
			super();
		}
		public function loadIn(_url:String,_result:Function):FLoader{
			this.result = _result;
			this.lastUrl  = _url;
			var _urlRequest:URLRequest = new URLRequest(_url);
			this.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);
			this.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			super.load(_urlRequest);
			return this;
		}
		override public function unload():void{
			trace("unload",this.lastUrl);
			this.removeListener();
			this.result = null;
			super.unload();
		}
		public function loadComplete(_e:Event):void{
			this.removeListener();
			if(this.result!=null){
				this.result(_e.target.content)
			}
		}
		private function removeListener():void{
			this.contentLoaderInfo.removeEventListener(Event.COMPLETE, loadComplete);
			this.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
		}
		public function ioErrorHandler(_e:IOErrorEvent):void{
//			trace("io error");
			throw(new Error("FLoader.io error, url: "+this.lastUrl));
		}
	}
}